

public class l19_删除链表的第N个节点二次版本 {
    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static void main(String[] args) {
        System.out.println("hello");
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode firstNode = head ;
        ListNode nextNode = head ;
        //  a - b - c
        int len = 1;
        while (firstNode.next != null){
            firstNode = firstNode.next;
            len += 1 ;
            if (len > n+1)
                nextNode = nextNode.next;
        }

        if (n == len){
            return  head.next ;
        }


        nextNode.next = nextNode.next.next;

        return head;


    }
}
